﻿using BaseUtils.Logger;
using SharpConfig;
using System;
using System.Collections.Generic;
using System.IO;

namespace NWMES.Model
{

    /// <summary>
    /// MES的基础类
    /// </summary>
    public class MesBaseEntity : IMesBaseEntity
    {

        public static string MESConfigFileName = "MESCFG.ini";
        public static string MESCFGPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + MESConfigFileName;
        public static Configuration config { get; set; }

        static MesBaseEntity()
        {
            Configuration.IgnoreInlineComments = true;
            RefreshConfig();

            if (config==null)
            {
                throw new Exception("严重错误，MES配置信息缺失");
            }
            string cfgLogFilePath = config["MesLog"]["SavePath"].RawValue;
            if (!string.IsNullOrWhiteSpace(cfgLogFilePath))
            {
                DefaultLogFileSavePath = cfgLogFilePath;
            }
        }

        public static void RefreshConfig()
        {
            if (!File.Exists(MESConfigFileName))
            {
                LoggerService.Error($"严重错误，缺少配置文件，请检查是否存在，地址：{MESCFGPath}");
            }
            else
            {
                config = Configuration.LoadFromFile(MESConfigFileName);
            }
        }

        /// <summary>
        /// 日志文件的默认保存地址
        /// 
        /// 可以在debug文件交下的 MESCFG.ini文件中去修改保存地址
        /// 修改 SavePath 即可
        /// 
        /// </summary>
        public static string DefaultLogFileSavePath = @"D:\MESLOG\";

        /// <summary>
        /// 接口
        /// </summary>
        public string Url { get; set; }

        public string ActionName { get; set; }

        public string RequestElementName { get; set; }

        /// <summary>
        /// 请求超时时间
        /// </summary>
        public int Timeout { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 操作用户
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// 基地站点
        /// </summary>
        public string Site { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string ActivityId { get; set; }

        public string remark { get; set; }

        /// <summary>
        /// 操作工序
        /// </summary>
        public string Operation { get; set; }
        public string OperationRevision { get; set; }

        /// <summary>
        /// 资源号
        /// </summary>
        public string Resource { get; set; }

        /// <summary>
        /// 数据数据组
        /// </summary>
        public string DcGroup { get; set; }
        public string DcGroupRevision { get; set; }

        /// <summary>
        /// 过站模式
        /// </summary>
        public string ModeProcessSFC { get; set; }

        public string dcMode { get; set; }
        public string dcGroupSequence { get; set; }
        public string material { get; set; }
        public string materialRevision { get; set; }

        public int sfcQty { get; set; } = 1;

        public List<MachineIntegrationParamData> parametricDataArray { get; set; }

        public static void CopyProperties(IMesBaseEntity Source, IMesBaseEntity Destiantion)
        {
            Destiantion.Url = Source.Url;
            Destiantion.Timeout = Source.Timeout;
            Destiantion.UserName = Source.UserName;
            Destiantion.Password = Source.Password;
            Destiantion.User = Source.User;
            Destiantion.Site = Source.Site;
            Destiantion.ActivityId = Source.ActivityId;
            Destiantion.remark = Source.remark;
            Destiantion.Operation = Source.Operation;
            Destiantion.OperationRevision = Source.OperationRevision;
            Destiantion.Resource = Source.Resource;
            Destiantion.DcGroup = Source.DcGroup;
            Destiantion.DcGroupRevision = Source.DcGroupRevision;
            Destiantion.ModeProcessSFC = Source.ModeProcessSFC;
            Destiantion.dcMode = Source.dcMode;
            Destiantion.dcGroupSequence = Source.dcGroupSequence;
            Destiantion.material = Source.material;
            Destiantion.materialRevision = Source.materialRevision;
            Destiantion.sfcQty = Source.sfcQty;
        }
    }
}
